package com.wang.tasker.core;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TaskPackage implements Runnable{

	private ArrayList<Task> taskList = new ArrayList<Task>();
	
	private String name;
	
	private String description="";
	
	private TaskContext context;
	
	private static Logger logger = LoggerFactory.getLogger(TaskPackage.class);
	
	public TaskPackage(){
	}
	
	public void loadPackage(String packageName){
		logger.info("--------------------------------------------------");
		logger.info("	Building Package: {}", packageName);
		logger.info("--------------------------------------------------");
		String jtaskerHome = System.getProperty("jtasker.home");
		logger.debug("JTasker Home: {}", jtaskerHome);
		String configPath = "workspace" + File.separator + packageName + File.separator + "package.xml";
		File packageConfig = new File(jtaskerHome + File.separator + configPath);
		if(!packageConfig.exists()){
			packageConfig = new File(configPath);
			if(!packageConfig.exists())
				throw new TaskerException("Load package Faild! Package file: " + packageConfig.getAbsolutePath());
		}
		
		SAXReader reader = new SAXReader();
		try {
			ConfigElement configEle = new ConfigElement(packageConfig);
			
			context = new TaskContext(this,configEle);
			
			File packageFolder = packageConfig.getParentFile();
			context.setWorkPath(packageFolder.getAbsolutePath());
			logger.debug("Package home: {}", context.getWorkPath());
			
			//package name
			ConfigElement nameEle = configEle.selectSingleElement("/Package/Name");
			this.name = (nameEle == null)?packageName:nameEle.getTextTrim();
			//package Description
			ConfigElement descriptionEle = configEle.selectSingleElement("/Package/Description");
			this.description = (descriptionEle == null)?"":descriptionEle.getTextTrim();
			//package Task
			List<ConfigElement> taskEleList = configEle.selectElements("/Package/TaskList/Task");
			logger.debug("Package have {} tasks...", taskEleList.size());
			for(int i=0;i<taskEleList.size();i++){
				ConfigElement taskEle = taskEleList.get(i);
				String name = taskEle.attributeValue("Name");
				String type = taskEle.attributeValue("Type");
				logger.debug("  {}. Task[{}@{}]", new String[]{(i+1)+"", name, type});
				Task task = this.context.getTask(type);
				task.setParent(this);
				task.load(taskEle);
				this.addTask(task);
			}
			logger.debug("--------------------------------------------------");
			
		} catch (DocumentException e) {
			throw new TaskerException("Load package Faild! XML Config file load faild.", e);
		}
	}
	
	public void addTask(Task task){
		taskList.add(task);
	}
	
	public void run(){
		for(Task task:taskList){
			if(task != null){
				this.context.setCurrentTask(task);
				task.setStatus("Running...");
				logger.info("--------------------------------------------------");
				logger.info("Start task: {}", task.getName());
				System.out.println();
				task.run();
				System.out.println();
			}
		}
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int size(){
		return taskList.size();
	}

	public TaskContext getContext() {
		return context;
	}



	
	
}